package com.currency.mall.ui.main.inquiry;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.adapter.GoldRateAdapter;
import com.currency.mall.entity.SharesBean;
import com.currency.mall.entity.SharesRoyalty;
import com.currency.mall.entity.SharesRoyaltyBean;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.ui.login.LoginActivity;
import com.currency.mall.ui.main.optional.OptionalDetailsActicity;
import com.currency.mall.ui.web.WebViewActivity;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/22.
 */

public class InquiryDetailsActicity extends BaseActivity {
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    @BindView(R.id.rl_tile)
    RelativeLayout rlTile;
    @BindView(R.id.tv_current_price)//当前价格
            TextView tvCurrentPrice;
    @BindView(R.id.tv_current)
    TextView tvCurrent;
    @BindView(R.id.tv_upsDownsRate)
    TextView tvUpsDownsRate;
    @BindView(R.id.tv_today_open_price)//今开
            TextView tvTodayOpenPrice;
    @BindView(R.id.tv_limit_up_price)//涨停价
            TextView tvLimitUpPrice;
    @BindView(R.id.tv_yesterday_collect_price)
    TextView tvYesterdayCollectPrice;
    @BindView(R.id.tv_limit_down_price)
    TextView tvLimitDownPrice;
    @BindView(R.id.btn_subscribe)
    Button btnSubscribe;
    @BindView(R.id.rv_gold_rate)
    RecyclerView rvGoldRate;
    private SharesBean.SharesInfo sharesInfo;
    private GoldRateAdapter mGoldRateAdapter;
    private ArrayList<SharesRoyalty> sharesRoyalties = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inquiry_details);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        sharesInfo = (SharesBean.SharesInfo) getIntent().getSerializableExtra("sharesInfo");
        init();
    }

    private void init() {
        setView();
        rvGoldRate.setLayoutManager(new LinearLayoutManager(getContext()));
        rvGoldRate.setNestedScrollingEnabled(false);
        mGoldRateAdapter = new GoldRateAdapter(null, getContext());
        rvGoldRate.setAdapter(mGoldRateAdapter);
        getRoyaltyQuery();
    }

    private void setView() {

        if (sharesInfo != null) {
            tvTitle.setText(sharesInfo.getName() + "(" + sharesInfo.getCode() + ")");
            tvCurrentPrice.setText(sharesInfo.getCurrentPrice());
            tvTodayOpenPrice.setText(sharesInfo.getOpeningPrice());
            tvLimitUpPrice.setText(sharesInfo.getHighestPrice());
            tvYesterdayCollectPrice.setText(sharesInfo.getClosingPrice());
            tvLimitDownPrice.setText(sharesInfo.getLowestPrice());
            double currentPrice = Double.parseDouble(sharesInfo.getCurrentPrice());
            double closingPrice = Double.parseDouble(sharesInfo.getClosingPrice());
            float upsDownsVal = (float) (currentPrice - closingPrice);
            if (upsDownsVal >= 0) {
                tvCurrent.setTextColor(getResources().getColor(R.color.optional_color));
            } else {
                tvCurrent.setTextColor(getResources().getColor(R.color.green_color));
            }

            if (currentPrice > closingPrice) {
                tvCurrentPrice.setTextColor(getResources().getColor(R.color.optional_color));
            } else {
                tvCurrentPrice.setTextColor(getResources().getColor(R.color.green_color));
            }

            tvCurrent.setText(upsDownsVal + "");
            tvUpsDownsRate.setText(sharesInfo.getUpsDownsRate() + "%");
            if (sharesInfo.getUpsDownsRate() >= 0) {
                tvUpsDownsRate.setTextColor(getResources().getColor(R.color.optional_color));
            } else {
                tvUpsDownsRate.setTextColor(getResources().getColor(R.color.green_color));
            }

        }

    }

    public void getRoyaltyQuery() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sharesCode", sharesInfo.getCode());
        NetWork.getOderApi().royaltyQuery(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SharesRoyaltyBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUitl.show(e.getMessage(), 200);
                    }

                    @Override
                    public void onNext(SharesRoyaltyBean sharesRoyaltyBean) {
                        if (sharesRoyaltyBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            setData(sharesRoyaltyBean);
                        } else {
                            ToastUitl.show(sharesRoyaltyBean.getMsg(), 200);
                        }
                    }
                });
    }

    private void setData(SharesRoyaltyBean sharesRoyaltyBean) {
        sharesRoyalties = sharesRoyaltyBean.getSharesRoyaltys().getList();
        mGoldRateAdapter.setNewData(sharesRoyaltyBean.getSharesRoyaltys().getList());
    }

    @OnClick({R.id.ll_back, R.id.btn_subscribe,R.id.tv_rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_subscribe:
                if (PreferenceUtils.getLoginStatus()) {
                    Intent intent = new Intent(getContext(), OptionalDetailsActicity.class);
                    intent.putExtra("sharesInfo", sharesInfo);
                    intent.putParcelableArrayListExtra("sharesRoyalties", sharesRoyalties);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tv_rule:
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("ExtraWebPath", "http://150.109.125.51/help/guide/rules.html");
                intent.putExtra("ExtraWebTitle", "规则");
                startActivity(intent);
                break;
        }
    }

}
